package com.zhanghu.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: zhanghu
 * @Description: cglib子类
 * @Date: Create in 9:47 2020/1/20
 * @modified by:
 */
public class ProxyFactory implements MethodInterceptor {
    private Object object;

    public ProxyFactory(Object o) {
        this.object = o;
    }


    public Object getProxy() {
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(this);
        enhancer.setSuperclass(object.getClass());
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("开始。。。。。");
        Object result = method.invoke(object, objects);
        System.out.println("结束。。。。。");
        return result;
    }
}
